package com.learn.ProducerConsumer;
/**
 * 生产这消费者问题
 * synchronized + wait + notifyAll  实现
 * synchronized实际上就是锁加排队
 */
public class SynPc {
    public static void main(String[] args){
        Way way = new Way();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> way.one()).start();
            new Thread(() -> way.two()).start();
        }
    }
}
class Way{
    public int num;
    public synchronized void one() {
        while(num==0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        num--;
        System.out.println(Thread.currentThread().getName()+"=>"+num);
        notifyAll();
    }
    public synchronized void two(){
        while(num!=0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        num++;
        System.out.println(Thread.currentThread().getName()+"=>"+num);
        notifyAll();
    }
}



